package com.ruoyi.ams.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.ams.domain.vo.AssetUnlockLog;
import com.ruoyi.ams.domain.vo.TreeSelect;
import com.ruoyi.ams.domain.vo.WxTreeSelect;
import com.ruoyi.ams.mapper.AmsOwnAddressMapper;
import com.ruoyi.ams.service.IAmsOwnAddressService;
import com.ruoyi.common.core.domain.model.AmsOwnAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资产所有方区域信息Service业务层处理
 *
 * @author yfkj
 * @date 2021-10-15
 */
@Service
public class AmsOwnAddressServiceImpl extends ServiceImpl<AmsOwnAddressMapper, AmsOwnAddress>  implements IAmsOwnAddressService
{
    @Autowired
    private AmsOwnAddressMapper amsOwnAddressMapper;

    /**
     * 查询资产所有方区域信息
     *
     * @param addressId 资产所有方区域信息主键
     * @return 资产所有方区域信息getUnlockLogs
     */
    @Override
    public AmsOwnAddress selectAmsOwnAddressByAddressId(Long addressId)
    {
        return amsOwnAddressMapper.selectAmsOwnAddressByAddressId(addressId);
    }

    /**
     * 查询资产所有方区域信息列表
     *
     * @param amsOwnAddress 资产所有方区域信息
     * @return 资产所有方区域信息
     */
    @Override
    public List<AmsOwnAddress> selectAmsOwnAddressList(AmsOwnAddress amsOwnAddress)
    {
        return amsOwnAddressMapper.selectAmsOwnAddressList(amsOwnAddress);
    }

    /**
     * 新增资产所有方区域信息
     *
     * @param amsOwnAddress 资产所有方区域信息
     * @return 结果
     */
    @Override
    public int insertAmsOwnAddress(AmsOwnAddress amsOwnAddress)
    {
        amsOwnAddress.setCreateTime(System.currentTimeMillis());
        return amsOwnAddressMapper.insertAmsOwnAddress(amsOwnAddress);
    }

    /**
     * 修改资产所有方区域信息
     *
     * @param amsOwnAddress 资产所有方区域信息
     * @return 结果
     */
    @Override
    public int updateAmsOwnAddress(AmsOwnAddress amsOwnAddress)
    {
        amsOwnAddress.setUpdateTime(System.currentTimeMillis());
        return amsOwnAddressMapper.updateAmsOwnAddress(amsOwnAddress);
    }

    /**
     * 批量删除资产所有方区域信息
     *
     * @param addressIds 需要删除的资产所有方区域信息主键
     * @return 结果
     */
    @Override
    public int deleteAmsOwnAddressByAddressIds(Long[] addressIds)
    {
        return amsOwnAddressMapper.deleteAmsOwnAddressByAddressIds(addressIds);
    }

    /**
     * 删除资产所有方区域信息信息
     *
     * @param addressId 资产所有方区域信息主键
     * @return 结果
     */
    @Override
    public int deleteAmsOwnAddressByAddressId(Long addressId)
    {
        return amsOwnAddressMapper.deleteAmsOwnAddressByAddressId(addressId);
    }
    @Override
    public List<TreeSelect> buildTreeSelect(List<AmsOwnAddress> types)
    {
        List<AmsOwnAddress> menuTrees = buildTree(types);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<WxTreeSelect> buildWxTreeSelect(List<AmsOwnAddress> list) {
        List<AmsOwnAddress> addressTrees = buildTree(list);
        return addressTrees.stream().map(WxTreeSelect::new).collect(Collectors.toList());
    }

    public List<AmsOwnAddress> buildTree(List<AmsOwnAddress> types)
    {
        List<AmsOwnAddress> returnList = new ArrayList<AmsOwnAddress>();
        List<Long> tempList = new ArrayList<Long>();
        for (AmsOwnAddress type : types)
        {
            tempList.add(type.getAddressId());
        }
        for (Iterator<AmsOwnAddress> iterator = types.iterator(); iterator.hasNext();)
        {
            AmsOwnAddress menu = (AmsOwnAddress) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(types, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = types;
        }
        return returnList;
    }

    @Override
    public HashMap<Long, AssetUnlockLog> getUnlockLogs(String addressName, List<Long> deptIds) {

        List<AssetUnlockLog> unlockLogs = amsOwnAddressMapper.getUnlockLogs(addressName, deptIds);
        HashMap<Long, AssetUnlockLog> assetUnlockLogMap = new HashMap<>(unlockLogs.size());
        for(AssetUnlockLog log : unlockLogs){
            assetUnlockLogMap.put(log.getAssetId(),log);
        }
        return assetUnlockLogMap;
    }

    private void recursionFn(List<AmsOwnAddress> list, AmsOwnAddress t)
    {
        // 得到子节点列表
        List<AmsOwnAddress> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AmsOwnAddress tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<AmsOwnAddress> getChildList(List<AmsOwnAddress> list, AmsOwnAddress t)
    {
        List<AmsOwnAddress> tlist = new ArrayList<AmsOwnAddress>();
        Iterator<AmsOwnAddress> it = list.iterator();
        while (it.hasNext())
        {
            AmsOwnAddress n = (AmsOwnAddress) it.next();
            if (n.getParentId().longValue() == t.getAddressId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AmsOwnAddress> list, AmsOwnAddress t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
